package com.atguigu.jucdemo.interactive;

import ch.qos.logback.core.util.TimeUtil;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// V1 synchronized + wait + notifyAll 传统老版本
/*class AirCondition //资源类（空调） OOP = Field + Method
{
    private int num=0;
    public  synchronized void increment() throws InterruptedException
    {
        while(num!=0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"=="+(++num));
        //TimeUnit.MILLISECONDS.sleep(1000);
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException
    {
        while(num==0){
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"=="+(--num));
        //TimeUnit.MILLISECONDS.sleep(1000);
        this.notifyAll();
    }
}*/

//V2 JUC新版，Lock + condition.await() + condition.signalAll()
class AirCondition //资源类（空调） OOP = Field + Method
{
    private int num=0;
    private Lock lock =new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() throws InterruptedException
    {
        lock.lock();
        try {
            while(num!=0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"=="+(++num));
            //TimeUnit.MILLISECONDS.sleep(1000);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    public void decrement() throws InterruptedException
    {
        lock.lock();
        try {
            while(num==0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"=="+(--num));
            //TimeUnit.MILLISECONDS.sleep(1000);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}

public class ProdConsumerDemo
{
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();
        new Thread(()-> {
                try {
                    for (int i = 0; i < 3; i++) {
                        airCondition.increment();
                        //TimeUnit.MILLISECONDS.sleep(2000);
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        , "张三").start();
        new Thread(()-> {
            try {
                for (int i = 0; i < 3; i++) {

                    airCondition.decrement();
                    //TimeUnit.MILLISECONDS.sleep(2000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"李四").start();

        new Thread(()-> {
            try {
                for (int i = 0; i < 3; i++) {
                    airCondition.increment();
                    //TimeUnit.MILLISECONDS.sleep(2000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
                , "王五").start();

        new Thread(()-> {
            try {
                for (int i = 0; i < 3; i++) {

                    airCondition.decrement();
                    //TimeUnit.MILLISECONDS.sleep(2000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"赵六").start();
    }

}
